package com.sky.service.admin.impl;

import com.sky.constant.RedisConstant;
import com.sky.mapper.admin.CategoryMapper;
import com.sky.pojo.Category;
import com.sky.service.admin.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description CategoryService
 * @Author 王富贵
 * @Date 2024-10-06
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> findAll() {
        return categoryMapper.findAll();
    }

    /**
     * 根据类型查询分类
     */
    @Autowired
    private RedisTemplate redisTemplate;

    public List<Category> list(Integer type) {
        //添加redis缓存
        List<Category> list =
                (List<Category>) redisTemplate.opsForValue().get(RedisConstant.CATEGORY_LIST + type);
        if (list == null || list.size() == 0) {
            list = categoryMapper.list(type);
            redisTemplate.opsForValue().set(RedisConstant.CATEGORY_LIST + type, list);

        }
        return list;
    }
}

